import request from '@/utils/request'
import {listAnimation} from "@/api/type/animation";
import {listBook} from "@/api/type/book";
import {listCaricature} from "@/api/type/caricature";
import {listDocumentary} from "@/api/type/documentary";
import {listMovie} from "@/api/type/movie";
import {listTeleplay} from "@/api/type/teleplay";
import {ref} from "vue";
import {listDetail} from "@/api/detail/detail";

// 除了detail之外的所有列表
export const getListWithOutDetail = async () => {
  const theList = ref({
    animationList: [],
    bookList: [],
    caricatureList: [],
    documentaryList: [],
    movieList: [],
    teleplayList: [],
  });

  try {
    // 使用 Promise.all 来并行获取所有列表数据
    await Promise.all([
      listAnimation().then(response => {
        theList.value.animationList = response.rows;
      }),
      listBook().then(response => {
        theList.value.bookList = response.rows;
      }),
      listCaricature().then(response => {
        theList.value.caricatureList = response.rows;
      }),
      listDocumentary().then(response => {
        theList.value.documentaryList = response.rows;
      }),
      listMovie().then(response => {
        theList.value.movieList = response.rows;
      }),
      listTeleplay().then(response => {
        theList.value.teleplayList = response.rows;
      })
    ]);
  } catch (error) {
    // 处理可能的错误
    console.error('An error occurred while fetching lists:', error);
  }

  return theList.value;
}

// 获取所有列表
export const getAllList = async () => {
  const theList = ref({
    detailList: [],
    animationList: [],
    bookList: [],
    caricatureList: [],
    documentaryList: [],
    movieList: [],
    teleplayList: [],
  });

  try {
    // 使用 async/await 等待 getListWithOutDetail 函数完成
    const otherLists = await getListWithOutDetail();

    // 将除了详情列表之外的其他列表赋值给 theList
    Object.keys(otherLists).forEach(key => {
      if (key !== 'detailList') {
        theList.value[key] = otherLists[key];
      }
    });

    // 获取详情列表
    // const detailResponse = await listDetail();
    // theList.value.detailList = detailResponse.rows;
    await listDetail().then(response => {
      theList.value.detailList = response.rows;
    })
  } catch (error) {
    // 处理可能的错误
    console.error('An error occurred while fetching lists:', error);
  }

  return theList.value;
}
